package com.mo.lib.utils

import android.app.Activity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity

/**
 * @ author：mo
 * @ data：2022/4/18:14:05
 * @ 功能：
 */

fun Activity.changeFragments1(layoutId: Int, fragmentArray: Array<Fragment>, index: Int) {
    val manager = (this as FragmentActivity).supportFragmentManager
    val beginTransaction = manager.beginTransaction()
    val fragmentsList = manager.fragments
    if (fragmentsList.isEmpty()) {
        beginTransaction.add(layoutId, fragmentArray[index])
    } else {
        if (fragmentArray[index].isAdded && fragmentArray[index].isVisible) {
            return
        }
        for (fragment in fragmentsList) {
            if (fragment.isVisible) {
                beginTransaction.hide(fragment)
            }
        }

        if (fragmentArray[index].isAdded) {
            beginTransaction.show(fragmentArray[index])
        } else {
            beginTransaction.add(layoutId, fragmentArray[index])
        }
    }
    beginTransaction.commit()
}
